package com.yinhe.music.yhmusic.db.dao;

import com.j256.ormlite.dao.Dao;
import com.yinhe.music.yhmusic.db.AbstractBaseDb;
import com.yinhe.music.yhmusic.db.DbHelper;
import com.yinhe.music.yhmusic.db.bean.PlayListDBEntity;
import com.yinhe.music.yhmusic.model.Music;

import java.util.List;
import java.util.concurrent.Callable;


/**
 * Created by wangyang on 17/3/18.
 */

public class PlayListEntityDAO extends AbstractBaseDb {

    private static final String TAG = PlayListEntityDAO.class.getSimpleName();
    private Dao dao;


    public PlayListEntityDAO(DbHelper dbHelper) {
        super(dbHelper);
        dao = getDao(PlayListDBEntity.class);
    }

    public void insertSongListSyn(final List<PlayListDBEntity> playListDBEntities) {
        new Thread(() -> {
            try {
                dao.callBatchTasks(() -> {
                    for (int i = 0; i < playListDBEntities.size(); i++) {
                        dao.createOrUpdate(playListDBEntities.get(i));
                    }
                    return null;
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void clearSongList() {
        try {
            dao.delete(dao.queryForAll());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void updateSong(final PlayListDBEntity recentEntity) {
        try {
            dao.update(recentEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertPlayList(PlayListDBEntity recentEntity) {
        try {
            dao.createOrUpdate(recentEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public PlayListDBEntity getSongBySongIdSyn(final long songID) {
        try {
            return (PlayListDBEntity) dao.queryForId(songID);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public List<PlayListDBEntity> getAllPlayListSyn() {
        try {
            return (List<PlayListDBEntity>) dao.queryForAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public void deleteSongBySongId(long songID) {
        try {
            dao.deleteById(songID);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public void deleteSongListByList(List<Music> list) {
        try {
            dao.delete(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
